package com.itheima.controller;

import com.itheima.entity.Result;
import com.itheima.pojo.SysDictData;
import com.itheima.service.SysDictDataService;
import com.itheima.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * Description:
 *字典数据相关接口
 * @author chenzhi
 * @date 2020/8/6
 */
@RestController
@RequestMapping("/sysDictData")
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class WebDictTypeController {
    @Autowired
    private SysDictDataService sysDictDataService;

    @Autowired
    private SysDictTypeService sysDictTypeService;

    @PostMapping("/getListByDictType")
    //根据字典类型获取字典数据
    public Result getListByDictType(@RequestParam("dictType") String dictType) {
        if(!dictType.equals("")) {

            String uid= sysDictTypeService.getDictTypeUid(dictType);

            List<SysDictData> sysDictDataList = sysDictDataService.getDictTypeList(uid);

            return new Result("success", sysDictDataList);
        }
        return new Result("error", "查询数据失败");
    }


    @PostMapping("/getListByDictTypeList")
    //根据字典类型数组获取字典数据
    public Result getListByDictTypeList(@RequestBody List<String> dictTypeList) {
        if (dictTypeList.size() > 0) {
            //获取到uid的数组放到集合中
            List<String> sysDictTypeUidList= sysDictTypeService.getDictTypeUidList(dictTypeList);
            List<SysDictData> sysDictDataList =new ArrayList<SysDictData>();
            for (String s : sysDictTypeUidList) {
                for (SysDictData sysDictData : sysDictDataService.getDictTypeList(s)) {
                    sysDictDataList.add(sysDictData);
                }
            }
            return new Result("success", sysDictDataList);
        }
        return new Result("error", "查询数据失败");
    }


}
